package com.datastructure.test.differentofbinary;

public class DifferentOfBinary {

    public static void main(String[] args) {
        DifferentOfBinary differentOfBinary = new DifferentOfBinary();
        System.out.println(differentOfBinary.countBitDiff(16807,282475249));
    }

    public int countBitDiff (int m, int n) {
        // write code here
        String binary = getBinary(m, 2);
        String binary1 = getBinary(n, 2);
        int result = compareString(binary,binary1);
        return result+Math.abs(binary.length()-binary1.length());
    }

    private int compareString(String binary, String binary1) {
        byte[] bytes = binary.getBytes();
        byte[] bytes1 = binary1.getBytes();
        int sum = 0;
        if(bytes.length>=bytes1.length){
            for (int i = 0; i < bytes1.length; i++) {
                if(bytes[i]!=bytes1[i]){
                    sum++;
                }
            }
        } else {
            for (int i = 0; i < bytes.length; i++) {
                if(bytes[i]!=bytes1[i]){
                    sum++;
                }
            }
        }
        return sum;
    }


    public String getBinary(int M,int N){
        int m = Math.abs(M);
        String res = "0123456789ABCDEF";
        StringBuilder stringBuilder = new StringBuilder();
        while (m>0){
            stringBuilder.append(res.charAt(m%N));
            m=m/N;
        }
        return stringBuilder.reverse().toString();
    }
}
